package top.hkyzf.study.leetcode.string;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/12 8:59
 * @version V1.0
 */
public class 段式回文_1147 {

    @Test
    public void testLongestDecomposition () {
        String text = "elvtoelvto";
        int k = longestDecomposition(text);
        System.out.println(k);
    }
    
    public int longestDecomposition(String text) {
        int k = 0;
        int length = 1;
        int prev = 0;
        int last = text.length()-1;
        for (int i = 0; i < text.length(); i++) {
            if (prev+length == last+1-length) {
                if (text.substring(prev, prev+length).equals(text.substring(last+1-length, last+1))) {
                    return k+2;
                }
                return k+1;
            }
            if (prev+length > last+1-length) {
                return k+1;
            }

            if (text.substring(prev, prev+length).equals(text.substring(last+1-length, last+1))) {
                prev = prev + length;
                last = last - length;
                length = 1;
                k += 2;
            } else {
                length++;
            }
        }
        return k;
    }
}
